package com.jiyi.widget.picselect;

import java.io.Serializable;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.utils.ActivityUtil;

public class PicFloderList_Activity extends ActivityBase implements
		OnClickListener {

	private AlbumHelper picHelper;
	private List<ImageBucket> floderList;
	private GridView gv_xc;
	private ImageBucketAdapter adapter;
	private int max;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choicepic);
		ActivityUtil.getInstances().addPicSelect(this);
		picHelper = AlbumHelper.getHelper();
		picHelper.init(getApplicationContext());
		floderList = picHelper.getImagesBucketList(true);
		
		setTop();

		
		max = getIntent().getIntExtra("max", 6);
		gv_xc = (GridView) findViewById(R.id.gv_xc);
		refreshAdapter();
		gv_xc.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(PicFloderList_Activity.this,
						Addpic_Activity.class);
				intent.putExtra("max", max);
				intent.putExtra("images",
						(Serializable) floderList.get(position).imageList);
				startActivity(intent);
			}
		});

	}

	/**   
	 * @author: HYM 
	 * @Title: setTop   
	 * @Description: TODO(这里用一句话描述这个方法的作用)    void 
	 * @date: 2017-6-12
	 */  
	private void setTop() {
		TextView tvBack = (TextView) findViewById(R.id.tvBack);
		tvBack.setText("");
		tvBack.setOnClickListener(this);
		TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
		tvTitle.setText("手机相册");
		findViewById(R.id.tvSearch).setVisibility(View.GONE);
	}

	private void refreshAdapter() {
		if (adapter == null) {
			adapter = new ImageBucketAdapter(PicFloderList_Activity.this,
					floderList);
			gv_xc.setAdapter(adapter);
		} else {
			adapter.refreshAD(floderList);
		}
	}

	@Override
	protected void onResume() {
		floderList = picHelper.getImagesBucketList(true);
		refreshAdapter();
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tvBack:
			finish();
			break;

		default:
			break;
		}
	}

}
